package com.bignerdranch.android.expresshelper.Adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.bignerdranch.android.expresshelper.Activity.ResultActivity;
import com.bignerdranch.android.expresshelper.Bean.ExpressBean;
import com.bignerdranch.android.expresshelper.Dao.ExpressDao;
import com.bignerdranch.android.expresshelper.R;
import com.bignerdranch.android.expresshelper.Utils.ExpressDataHelper;
import com.bignerdranch.android.expresshelper.Utils.MyImageCache;
import com.bignerdranch.android.expresshelper.Utils.RequestUtil;
import com.squareup.picasso.Picasso;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.io.PipedReader;
import java.util.List;

/**
 * Created by Administrator on 2016/6/27.
 */
public class HistoryAdapter extends BaseAdapter {
    private Context mContext;
    private LayoutInflater mInflater;
    private List<ExpressBean> mDate;
    private ExpressDao mDao;
    View view;
    private ScrollView mScrollView;
    private LinearLayout mLinearLayout;


    public HistoryAdapter(Context context,View view, List<ExpressBean> mDate) {
        mContext = context;
        mInflater = LayoutInflater.from(context);
        mDao= new ExpressDao(context);
       /* mDate = mDao.queryAll();*/
        this.mDate = mDate;

        this.view =view;
        mScrollView = (ScrollView) view.findViewById(R.id.history_list);
        mLinearLayout = (LinearLayout)view.findViewById(R.id.nothing);
    }

    public String getNum(int index){
        return mDate.get(index).getExpressNum();
    }
    public String getSimName(int index){
        return mDate.get(index).getExpressIcon();
    }




    @Override
    public int getCount() {
        return mDate.size();
    }

    @Override
    public Object getItem(int position) {

            return mDate.get(position);

    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        if (convertView == null){
            convertView = mInflater.inflate(R.layout.swipelist_item,null);
        }
        ViewHandler handler = new ViewHandler();

        if (convertView.getTag() != null){
            handler = (ViewHandler) convertView.getTag();
        }else {
            handler.title = (TextView)convertView.findViewById(R.id.title_text);
            handler.num = (TextView)convertView.findViewById(R.id.num_text);

            handler.img = (ImageView)convertView.findViewById(R.id.image_item);
            handler.mButton = (Button)convertView.findViewById(R.id.remove);
            handler.remark = (TextView)convertView.findViewById(R.id.remark);
            handler.status = (TextView)convertView.findViewById(R.id.state_item);
            handler.map = (ImageView)convertView.findViewById(R.id.map_status);

            convertView.setTag(handler);
        }

        handler.title.setText(mDate.get(position).getExpressName());
        handler.num.setText(mDate.get(position).getExpressNum());
        File file = new File(mContext.getFilesDir(),mDate.get(position).getExpressIcon()+".jpg");
      /*  Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath()+".jpg");
        handler.img.setImageBitmap(bitmap);*/
        Picasso.with(mContext)
                .load(file)
                .fit()
                .into(handler.img);
        handler.mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                mDao.delete(mDate.get(position).getExpressNum());
                mDate.remove(position);
                notifyDataSetChanged();
                if (mDate.size()>0){
                    mScrollView.setVisibility(View.VISIBLE);
                    mLinearLayout.setVisibility(View.INVISIBLE);
                }else {
                    mScrollView.setVisibility(View.INVISIBLE);
                    mLinearLayout.setVisibility(View.VISIBLE);
                }
            }
        });
        handler.remark.setText(mDate.get(position).getExpressNick());

        if (mDate.size()>0){
            switch (mDate.get(position).getExpressStatus()+""){
                case "1":
                    handler.status.setText("暂无记录");
                    handler.map.setImageResource(R.drawable.map);
                    break;
                case "2":
                    handler.status.setText("在途中");
                    handler.map.setImageResource(R.drawable.map);
                    break;
                case "3":
                    handler.status.setText("派送中");
                    handler.map.setImageResource(R.drawable.map);
                    break;
                case "4":
                    handler.status.setText("已签收");
                    handler.map.setImageResource(R.drawable.map_green);
                    break;
                default:
                    handler.status.setText("查询异常");
                    handler.map.setImageResource(R.drawable.map);
                    break;
            }
        }else {
            Toast.makeText(mContext,"没有历史记录",Toast.LENGTH_SHORT).show();
        }


        return convertView;
    }

    private static class ViewHandler{
        public ImageView img;
        public TextView title;
        public TextView num;

        public Button mButton;

        public TextView remark;
        public TextView status;
        public ImageView map;

    }
}
